package edu.nf.llmtest.service.impl;

import edu.nf.llmtest.common.VO.ResultVO;
import edu.nf.llmtest.common.annotation.SyncData;
import edu.nf.llmtest.common.converter.UserConverter;
import edu.nf.llmtest.common.enumeration.ActionType;
import edu.nf.llmtest.common.enumeration.RedisKeyEnum;
import edu.nf.llmtest.common.enumeration.TargetType;
import edu.nf.llmtest.common.sync.syncableService;
import edu.nf.llmtest.mapper.UserMapper;
import edu.nf.llmtest.model.document.UserDoc;
import edu.nf.llmtest.model.entity.User;
import edu.nf.llmtest.service.Listenter.MqProducer;
import edu.nf.llmtest.service.UserService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ Author bin
 * @ Create  2025/10/20 下午7:15
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl implements UserService , syncableService<User> {
    private final UserMapper userMapper;

    private final RedisTemplate<String, Object> redisTemplate;

    private final MqProducer mqProducer;

    private final ElasticsearchOperations operations;

    private final UserConverter userConverter;

    @PostConstruct
    public void init() {
        List<User> list = userMapper.listUserAll();
        for (User user : list) {
            //以用户邮箱为key用户信息为value
            redisTemplate.opsForValue().set(RedisKeyEnum.USER_EMAIL.getValue() + user.getEmail(), user);
            //转换并添加到es中
            UserDoc userDoc = userConverter.convertUserDoc(user);
            operations.save(userDoc);
        }
    }

    @Override
    public User UserIsExist(String email) {
        User user;

        //先从缓存中查询
        user = (User) redisTemplate.opsForValue().get(RedisKeyEnum.USER_EMAIL.getValue() + email);
        //如果缓存中有则返回
        if (user != null)
            return user;

        //如果es中有则返回
        UserDoc userDoc = operations.get(email, UserDoc.class);
        if (userDoc != null)
            return userConverter.convertUser(userDoc);

        user =  userMapper.findUserByEmail(email);
        if (user != null){
            //缓存用户信息
            redisTemplate.opsForValue().set(RedisKeyEnum.USER_EMAIL.getValue() + email, user);
            return user;
        }else
            return null;
    }

    @Override
    public User login(String email, String password) {
        User user = UserIsExist(email);
        log.info("用户登录检查信息{}", user);
        if(user != null && user.getPassword().equals(password)){
            return user;
        } else if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        } else{
            throw new RuntimeException("用户不存在");
        }

    }

    @Transactional
    @Override
    @SyncData(action = ActionType.UPDATE, targets = {TargetType.ES, TargetType.REDIS})
    public void register(String email, String password) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        mqProducer.send("registerUser", user);
    }

    @Transactional
    @Override
    @SyncData(action = ActionType.DELETE, targets = {TargetType.ES, TargetType.REDIS})
    public void deleteUser(String email) {
        User user = new User() ;
        user.setEmail(email);
        mqProducer.send("delUser", user);
    }


}
